<template>
  <template v-if="getShow">
    <LoginFormTitle class="enter-x" />
    <Form class="p-4 enter-x" :model="formData" :rules="getFormRules" ref="formRef">
      

      <FormItem name="mobile" class="enter-x">
        <Input size="large" v-model:value="formData.mobile" :placeholder="t('sys.login.mobile')" />
      </FormItem>
      <FormItem name="upwdValidCode" class="enter-x">
        <CountdownInput
          size="large"
          v-model:value="formData.upwdValidCode"
          :sendCodeApi="yztelnumandsend"
          :placeholder="t('sys.login.smsCode')"
        />
      </FormItem>

     

      <FormItem name="password" class="enter-x">
        <StrengthMeter
          size="large"
          v-model:value="formData.password"
          :placeholder="t('sys.login.password')"
        />
      </FormItem>
      <FormItem name="confirmPassword" class="enter-x">
        <InputPassword
          size="large"
          visibilityToggle
          v-model:value="formData.confirmPassword"
          :placeholder="t('sys.login.confirmPassword')"
          autocomplete="false"
        />
      </FormItem>

      <FormItem class="enter-x">
        <Button type="primary" size="large" block @click="handleReset" :loading="loading">
          {{ t('common.resetText') }}
        </Button>
        <Button size="large" block class="mt-4" @click="handleBackLogin">
          {{ t('sys.login.backSignIn') }}
        </Button>
      </FormItem>
    </Form>
  </template>
</template>
<script lang="ts" setup>
  import { reactive, ref, computed, unref } from 'vue';
  import LoginFormTitle from './LoginFormTitle.vue';
  import { Form, Input, Button } from 'ant-design-vue';
  import { CountdownInput } from '/@/components/CountDown';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useLoginState, useFormRules, LoginStateEnum,useFormValid } from './useLogin';
  import { StrengthMeter } from '/@/components/StrengthMeter';
  import { getRegSmscode, savePwdByValidCodeNew } from '/@/api/sys/account';
  const formRef = ref();
  const { showMessage, notification } = useMessage();
  const FormItem = Form.Item;
  const { t } = useI18n();
  const { handleBackLogin, getLoginState } = useLoginState();
 
  const InputPassword = Input.Password;

  
  const loading = ref(false);

  const formData = reactive({
    loginCode: '',
    password: '',
    confirmPassword: '',
    mobile: '',
    upwdValidCode: '',
  });
  const { getFormRules } = useFormRules(formData);
  const { validForm } = useFormValid(formRef);
  const getShow = computed(() => unref(getLoginState) === LoginStateEnum.RESET_PASSWORD);

  async function handleReset() {
    loading.value=true;
    try {
      formData.loginCode=formData.mobile;
      const data = await validForm();
      if (!data) return;
      const res = await savePwdByValidCodeNew(formData);
      
      if (res.result == 'true') {
       
        notification.success({
            message: '修改密码成功',
            description: '您可以使用新密码登录系统',
            duration: 3,
          });
          //清空
            formData.loginCode='';
            formData.mobile='';
            formData.upwdValidCode='';
            formData.password='';
            formData.confirmPassword='';

            //返回登录框
            handleBackLogin();
      }
    }catch(error: any){

    }finally{
      loading.value=false;
    }
  }

  const yztelnumandsend = async () => {
       const regex = /^1[3-9]\d{9}$/; // 中国大陆手机号码正则表达式
      if (regex.test(formData.mobile.trim())) {
          const res = await getRegSmscode({telnum:formData.mobile.trim(),type:'upwd'});
          if(res.result=='true'){
            return true;
          }else{
            return false;
          }
          
      }else{
        showMessage('请填写正确的手机号');
      }
       return false;
  };
</script>
